package com.example.server;

import java.util.Scanner;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import app.database.friendlist.FriendListDB;

public class DialogIntent extends Activity{
	String message;
	String callerName;
	String callerEmail;
	
	public class OutgoingReceiver extends BroadcastReceiver {
		public static final String CUSTOM_INTENT = "com.example.server.DialogIntent";
		
		public OutgoingReceiver(int permission){
			System.out.println("HIT OUTGOING");
			Intent i = new Intent();
			i.setAction(CUSTOM_INTENT);
			i.putExtra("PERMISSION", permission);
			DialogIntent.this.sendBroadcast(i);
			Log.v("broadcast",CUSTOM_INTENT);
		}
		@Override
		public void onReceive(Context context, Intent intent) {
			System.out.println("HIT OUTGOING");
			Intent i = new Intent();
			i.setAction(CUSTOM_INTENT);
			context.sendBroadcast(i);
			} 
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		message = getIntent().getStringExtra("MESSAGE");
        Scanner sc=new Scanner(message).useDelimiter("\\|");
		String s1,s2;
		callerName=sc.next();
		callerEmail=sc.next();

		AlertDialog dialog =   new AlertDialog.Builder(this).create();
		message = "User: "+callerName+"\nEmail: "+callerEmail
		+"\nis requesting your location.\nDo you grant the permission?";
		dialog.setMessage(message);
		dialog.setButton(DialogInterface.BUTTON_POSITIVE, "YES", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				final int TABLE = 1;
				FriendListDB dba = new FriendListDB(DialogIntent.this);
				dba.open();
				dba.insertFriend(callerName, callerEmail, "", "", TABLE);
				dba.close();
				OutgoingReceiver o = new OutgoingReceiver(1);
				// TODO Auto-generated method stub
				finish();
			}
		});
		dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "NO", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				OutgoingReceiver o = new OutgoingReceiver(0);
				finish();
			}
		});
		dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface arg0) {
				// TODO Auto-generated method stub
				OutgoingReceiver o = new OutgoingReceiver(0);
				finish();
				//System.out.println("Dialog on BackPress");
			}
		});
		dialog.show();
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		//Toast.makeText(this, "on Finish", Toast.LENGTH_SHORT).show();
        //System.out.println("Activity on Finsh");
		super.finish();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		//Toast.makeText(this, "on Destroy", Toast.LENGTH_SHORT).show();
		//System.out.println("Activity on Destroy");
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		//Toast.makeText(this, "on BackPress", Toast.LENGTH_SHORT).show();
		//System.out.println("Activity on BackPressed");
		super.onBackPressed();
	}
	
	

}
